package com.epfresh.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.constant.AppPurchaseRouterConstant;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.bean.WalletRecordEntity;
import com.epfresh.constant.Constant;
import com.epfresh.global.BaseActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyWalletActivity extends  BaseActivity {
    //private View tvAbout;
    private TextView tvSum;

    private TextView tvTime;
    private ListView lvTime;
    private ListView lvContent;
    ArrayList<WalletRecordEntity> bankRecords = new ArrayList<>();
    boolean timeFlag;
    private BaseAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setImageResource(R.mipmap.title_left_white);
        actionbar.setBackgroundResource(R.color.green_little);
        actionbar.findViewById(R.id.v_line).setVisibility(View.GONE);
        toolbarTitle.setTextColor(Color.WHITE);
        rightbtn.setTextColor(Color.WHITE);
        toolbarTitle.setText("我的钱包");
        rightbtn.setText("钱包说明");
        rightbtn.setVisibility(View.VISIBLE);
        rightbtn.setTextColor(Color.WHITE);
        rightbtn.setOnClickListener(this);

        tvSum = (TextView)findViewById(R.id.tv_sum);
/*        tvAbout = findViewById(R.id.tv_about);
        tvAbout.setOnClickListener(this);*/
        mHttpLoad();


        tvTime = (TextView)findViewById(R.id.tv_time);
        lvTime = (ListView)findViewById(R.id.lv_time);
        lvContent = (ListView)findViewById(R.id.lv_content);
        View rlNoDate = findViewById(R.id.rl_nodate);
        lvContent.setEmptyView(rlNoDate);
        tvTime.setOnClickListener(this);
        initTimeList();
        initAdapter();

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_time:
                if(timeFlag){
                    lvTime.setVisibility(View.GONE);
                    timeFlag = false;
                }else {
                    timeFlag = true;
                    lvTime.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.rightbtn:
                Intent pro = new Intent(this, WebActivity.class);
                pro.putExtra(Constant.KEY_WEB_URL, com.epfresh.api.constant.Constant.URL_HELP_WALLET);
                pro.putExtra(Constant.KEY_WEB_TITLE, "钱包说明");
                //pro.putExtra("title","结算历程及规则");
                startActivity(pro);
                break;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    /**
     * 加载收入数据
     */
    private void mHttpLoad() {
        RequestEntityPurchase<Map<String, String>> data = new RequestEntityPurchase<>();
        data.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_coupon_wallet_amount);
        request(data, com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_coupon_wallet_amount, onRequestListener);
    }
    OnRequestListener<Map<String, String>> onRequestListener = new OnRequestListener<Map<String, String>>() {
        @Override
        public Map<String, String> jsonToObj(String s) {
            return new Gson().fromJson(s, new TypeToken<Map<String, String>>() {
            }.getType());
        }
        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<Map<String, String>> response, Object tag) {

            hideProgressDialog();
            Map<String, String> responseElement = response.getResponseElement();
            tvSum.setText(responseElement.get("result"));
        }
    };

    private void initAdapter() {
        adapter = new CommonAdapter<WalletRecordEntity>(this, bankRecords,R.layout.item_record) {
            @Override
            public void convert(final com.epfresh.api.adapter.ViewHolder holder, final WalletRecordEntity bean) {
                holder.setText(R.id.tv_time, bean.getTime());
                holder.setText(R.id.tv_money, bean.getUsed()+"元");
            }
        };
        lvContent.setAdapter(adapter);
        mHttpLoad(30);
    }


    void initTimeList(){
        final ArrayList<String> times = new ArrayList<>();
        /*for (int i = 0;i<5;i++){
            times.add(i,"近"+(i+1)+"个月");
        }*/
        times.add(0,"近"+1+"个月");
        times.add(1,"近"+3+"个月");
        times.add(2,"近"+1+"年");
        lvTime.setAdapter(new CommonAdapter<String>(this, times, R.layout.item_string) {
            @Override
            public void convert(final com.epfresh.api.adapter.ViewHolder holder, final String bean) {
                holder.setText(R.id.tv_string, bean);
                holder.setOnClickListener(R.id.tv_string, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvTime.setText(bean);
                        lvTime.setVisibility(View.GONE);
                        timeFlag = false;
                        if(times.indexOf(bean)==0){
                            mHttpLoad(30);
                        }else if (times.indexOf(bean)==1){
                            mHttpLoad(90);
                        }else {
                            mHttpLoad(365);
                        }
                        //mHttpLoad((times.indexOf(bean)+1));
                    }
                });
            }
        });
    }
/*

    */
    /**
     * 加载收入数据
     */

    private void mHttpLoad(int i) {
        RequestEntityPurchase<Map<String, String>> data = new RequestEntityPurchase<>();
        data.setCmd(AppPurchaseRouterConstant.cmd_coupon_wallet_records);
        Map<String, String> map = new HashMap<>();
        map.put("days", i+"");
        data.setParameters(map);
        request(data, AppPurchaseRouterConstant.cmd_coupon_wallet_records, onRequestListListener);
    }

    private OnRequestListener<List<WalletRecordEntity>> onRequestListListener = new OnRequestListener<List<WalletRecordEntity>>() {
        @Override
        public List<WalletRecordEntity> jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, new TypeToken<List<WalletRecordEntity>>() {
            }.getType());
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<List<WalletRecordEntity>> response, Object tag) {

            hideProgressDialog();
            List<WalletRecordEntity> responseElement = response.getResponseElement();
            bankRecords.clear();
            bankRecords.addAll(responseElement);
            adapter.notifyDataSetChanged();
        }
    };

}
